import request from "@/utils/http";
import type {
  AvatarVo,
  LoginForm,
  Oauth2Token,
  ProfileVo,
  UpdateProfileVo,
  Result,
} from "@/types";
import { clientId } from "@/config";

const user = {
  login(data: LoginForm) {
    const params = {
      ...data,
      clientId: clientId,
      grantType: data.grantType || "password",
    };
    return request.post("/auth/login", params, {
      isToken: false,
      isEncrypt: true,
    }) as Promise<Result<Oauth2Token>>;
  },
  logout() {
    return request.post("/auth/logout") as Promise<Result<void>>;
  },
  getUserInfo() {
    return request.get("/system/user/profile", {}, {}, false) as Promise<
      Result<ProfileVo>
    >;
  },
  updateAvatar(filePath: string) {
    return request.upload(
      "/system/user/profile/avatar",
      filePath,
      "avatarfile"
    ) as Promise<Result<AvatarVo>>;
  },
  updateProfile(data: UpdateProfileVo) {
    return request.put("/system/user/profile", data) as Promise<Result<void>>;
  },
  updatePassword(data: { oldPassword: string; newPassword: string }) {
    return request.put("/system/user/profile/updatePwd", data, {
      isEncrypt: true,
    }) as Promise<Result<void>>;
  },
};

export default user;
